#include "../../mqServer/mq_exchange.hpp"
#include <gtest/gtest.h>

using namespace mq;

mq::ExchangeManager::manager_ptr emp;

class ExchangeTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        cout << "所有单元测试前执行环境初始化" << endl;
        emp = make_shared<ExchangeManager>("./data/meta.db");
    }
    virtual void TearDown() override
    {
        cout << "所有单元测试后执行环境清理" << endl;
        emp->clear();
    }
};

TEST(exchange_test, insert_test)
{
    // unordered_map<string, string> args = {{"k1", "v1"}, {"k2", "v2"}};
    unordered_map<string, string> args;
    emp->declareExchange("exchange1", ExchangeType::DIRECT, true, false, args);
    emp->declareExchange("exchange2", ExchangeType::FANOUT, true, false, args);
    emp->declareExchange("exchange3", ExchangeType::TOPIC, false, true, args);
    emp->declareExchange("exchange4", ExchangeType::DIRECT, true, false, args);
    ASSERT_EQ(emp->size(), 4);
}

TEST(exchange_test, remove_test)
{
    emp->deleteExchange("exchange4");
    ASSERT_EQ(emp->size(), 3);
    ASSERT_EQ(emp->exists("exchange4"), false);
}

TEST(exchange_test, select_test)
{
    ex_ptr exp = emp->selectExchange("exchange2");
    ASSERT_NE(exp.get(), nullptr);
    ASSERT_EQ(exp->_name, "exchange2");
    ASSERT_EQ(exp->_type, mq_message::ExchangeType::FANOUT);
    ASSERT_EQ(exp->_durable, true);
    ASSERT_EQ(exp->_auto_delete, false);
    // ASSERT_EQ(exp->getArgs(), string("k2=v2&k1=v1"));
    cout << exp->getArgs() << endl;
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new ExchangeTest);
    RUN_ALL_TESTS();
    return 0;
}